﻿/*
* 在这里定义流程需要的节点类型
*/

XWFActivityFactory.register('XWFBegin', function () { return new XWFBegin(); });
XWFActivityFactory.register('XWFEnd', function () { return new XWFEnd(); });
XWFActivityFactory.register('XWFProcess', function () { return new XWFProcess(); });
XWFActivityFactory.register('XWFDecision', function () { return new XWFDecision(); });

// 活动的蒙版
function XWFActivityMask() {
    XNode.apply(this, { hitTestVisible: false });
    this.addShape(XEngine._gi.createRectangle(), null, null, null, null, null, null, true);
}

// 动作蒙版
function XWFActionMask() {
    this._line = XEngine._gi.createLine();
    XElement.apply(this, [this._line._domEl]);

    this._fromActivity = null;
    this._toActivity = null;
    this._toPos = null;

    this.getFromActivity = function () {
        return this._fromActivity;
    }

    this.getToActivity = function () {
        return this._toActivity;
    }

    this.setFromActivity = function (fromActivity) {
        this._fromActivity = fromActivity;
        this.update();
    }

    this.setToActivity = function (toActivity) {
        this._toPos = null;
        this._toActivity = toActivity;
        this.update();
    }

    this.setToPos = function (x, y) {
        if (this._toPos == null) {
            this._toPos = { x: x, y: y };
        }
        else {
            this._toPos.x = x;
            this._toPos.y = y;
        }
        this._toActivity = null;
        this.update();
    }

    this.update = function () {
        if (this._fromActivity == null) {
            this._line.setPoints(null);
        }
        else {
            if (this._toActivity == null) {
                this._line.setPoints(XPathUtil.nodeToPos(this._fromActivity, this._toPos));
            } else {
                this._line.setPoints(XPathUtil.nodeToNode(this._fromActivity, this._toActivity));
            }
        }
    }

    // #override
    this.install = function (diagram) {
        diagram._domEl.appendChild(this._line._domEl);
    }

    // #override
    this.uninstall = function (diagram) {
        diagram._domEl.removeChild(this._line._domEl);
    }
}

//动作
//@fromActivity:XWFActivity
//@toActivity:XWFActivity
function XWFAction(fromActivity, toActivity) {
    XLink.apply(this, [fromActivity, toActivity]);
}

function XWFActivity(config) {
    XNode.apply(this, []);

    this.setSize(config.width, config.height);
}

function XWFBegin() {
    XWFActivity.apply(this, [{ width: 50, height: 50 }]);

    this.addShape(XEngine._gi.createRectangle(), null, null, null, null, null, null, true);
}

function XWFEnd() {
    XWFActivity.apply(this, [{ width: 50, height: 50 }]);

    this.addShape(XEngine._gi.createRectangle(), null, null, null, null, null, null, true);
}

function XWFProcess() {
    XWFActivity.apply(this, [{ width: 75, height: 50 }]);

    this.addShape(XEngine._gi.createRectangle(), null, null, null, null, null, null, true);
    this.addShape(XEngine._gi.createRectangle(), 10, 10, null, null, 10, 10);
    this.addShape(XEngine._gi.createRectangle(), 10, 30, null, null, 5, 5);
    this._oTextName = this.addShape(XEngine._gi.createText(name), null, null, null, null, 20, 10);
    this.addShape(XEngine._gi.createDiamond(), 50, 50, null, null, 20, 20);

    //#override
    this.onNameChanged = function (name) {
        this._oTextName.setText(name);
    }
}

function XWFDecision() {
    XWFActivity.apply(this, [{ width: 75, height: 50 }]);

    this.addShape(XEngine._gi.createRectangle(), null, null, null, null, null, null, true);
    this.addShape(XEngine._gi.createRectangle(), -6, 10, null, 20, 15, 15);
    this.addShape(XEngine._gi.createRectangle(), null, 1, 1, 20, 5, 5);
    this.addShape(XEngine._gi.createRectangle(), null, null, 1, 20, 5, 5);
    this.addShape(XEngine._gi.createRectangle(), 1, null, null, 20, 5, 5);
    this.addShape(XEngine._gi.createRectangle(), null, null, null, 20, 5, 5);
    this.addShape(XEngine._gi.createText('text'), null, null, null, 0, 20, 10);
    this.addShape(XEngine._gi.createDiamond(), 50, null, null, 0, 20, 20);
    this.addShape(XEngine._gi.createDiamond(), 150, null, null, 0, 20, 20);
    this.addShape(XEngine._gi.createDiamond(), 100, 100, null, 0, 50, 100);
}